<?php

class Designers_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    //BREADCUMBS
    function get_breadcumbs($cat_id){
        $cumb = '';
        $curr = true;
        while($cat_id != 0){
            $this->db->where('products_cat_id',$cat_id);
            $result = $this->db->get('products_cat')->first_row();
            if($curr){
                $cumb = ' - '.$result->products_cat_title.$cumb;
                $curr = false;
            }
            else{
                $cumb = ' - <a href="'.base_url().'products/cat/'.$result->products_cat_id.'">'.$result->products_cat_title.'</a>'.$cumb;
            }
            $cat_id = $result->products_cat_parent;
        }
        $cumb = '<a href="'.base_url().'products">PRODUCTS</a>'.$cumb;
        return $cumb;
    }
    
    
    //DESIGNERS
    public function get_designers($page=0){
        $offset = 20;
        $start = $page*$offset;
        $this->db->limit($offset,$start);
        $this->db->order_by('modified_date','desc');
        $this->db->join('nationality', 'nationality_id = designers.nationality');
        return $this->db->get('designers');
    }
    
    public function get_designers_front($prefix=null,$page=0){
        $offset = 20;
        $start = $page*$offset;
        $this->db->select('id');
        $this->db->select('nick_name');
        $this->db->select('city');
        $this->db->select('main_photo');
        if(!empty($prefix)){
            $this->db->like('nick_name',$prefix,'after');
        }
        $this->db->limit($offset,$start);
        $this->db->order_by('id','desc');
        return $this->db->get('designers');
    }
    
    public function get_designers_count(){
        return $this->db->count_all_results('designers');
    }
    
    public function get_designer($id){
        $this->db->where('id',$id,false);
        $result = $this->db->get('designers');
        if($result->num_rows() > 0){
            return $result->first_row();
        }else{
            return null;
        }
    }
    
    public function get_designer_front($id){
        $this->db->where('id',$id,false);
        $this->db->join('nationality', 'nationality_id = designers.nationality');
        $result = $this->db->get('designers');
        if($result->num_rows() > 0){
            $designer = $result->first_row();
            $designer->photo = $this->get_designer_photo($id);
            return $designer;
        }else{
            return null;
        }
    }
    
    public function get_designer_photo($id){
        $this->db->where('designers_id',$id,false);
        return $this->db->get('designers_photo');
    }
    
    public function search_designer($name){
        
    }
    
    public function add($input){
        //another field
        $this->db->set('nick_name',$input['nick_name']);
        $this->db->set('full_name',$input['full_name']);
        $this->db->set('birthdate',$input['birthdate']);
        $this->db->set('nationality',$input['nationality'],FALSE);
        $this->db->set('city',$input['city']);
        $this->db->set('gender',$input['gender']);
        $this->db->set('main_photo',$input['main_photo']);
        $this->db->set('contact1',$input['contact1']);
        $this->db->set('contact2',$input['contact2']);
        $this->db->set('website1',$input['website1']);
        $this->db->set('website2',$input['website2']);
        $this->db->set('description',$input['description']);
        $this->db->set('modified_date','NOW()',FALSE);
        $this->db->insert('designers');
    }
    public function edit($input){
        //main image
        if(!empty($input['main_photo'])){
            $this->delete_main_photo($input['id']);
            $this->db->set('main_photo',$input['main_photo']);
        }
        //another field
        $this->db->set('nick_name',$input['nick_name']);
        $this->db->set('full_name',$input['full_name']);
        $this->db->set('birthdate',$input['birthdate']);
        $this->db->set('nationality',$input['nationality'],FALSE);
        $this->db->set('city',$input['city']);
        $this->db->set('gender',$input['gender']);
        $this->db->set('contact1',$input['contact1']);
        $this->db->set('contact2',$input['contact2']);
        $this->db->set('website1',$input['website1']);
        $this->db->set('website2',$input['website2']);
        $this->db->set('description',$input['description']);
        $this->db->set('modified_date','NOW()',FALSE);
        $this->db->where('id',$input['id']);
        $this->db->update('designers');
    }
    
    public function delete($id){
        $this->photo_delete_by_designers($id);
        $this->delete_main_photo($id);
        $this->db->where('id',$id,FALSE);
        $this->db->delete('designers');
    }
    
    public function delete_main_photo($designers_id){
        $this->db->where('id',$designers_id,FALSE);
        $designer = $this->db->get('designers')->first_row();
        $path ='./photo/designers/main/'.$designer->main_photo;
        unlink($path);
    }
    
    
    
    //NATIONALITY
    public function get_nationality(){
        $this->db->order_by('nationality_title','asc');
        return $this->db->get('nationality');
    }
    
    //PHOTO
    public function photo_add($input){
        //set
        $this->db->set('designers_id',$input['designers_id'],FALSE);
        $this->db->set('link',$input['photo']);
        $this->db->insert('designers_photo');
    }
    
    public function photo_delete($id){
        $this->db->where('id',$id);
        $result = $this->db->get('designers_photo');
        $path ='./photo/designers/'.$result->first_row()->link;
        $this->db->where('id',$id);
        $this->db->delete('designers_photo');
        unlink($path);
    }
    
    public function photo_delete_by_designers($designers_id){
        $this->db->where('designers_id',$designers_id);
        $result = $this->db->get('designers_photo');
        foreach($result->result() as $photo){
            $this->photo_delete($photo->id);
        }
    }
}